package cn.chiship.framework.business.biz.business.controller;


import cn.chiship.framework.business.biz.business.pojo.dto.OrderCreateDto;
import cn.chiship.framework.common.annotation.SystemOptionAnnotation;
import cn.chiship.framework.common.enums.BusinessTypeEnum;
import cn.chiship.sdk.framework.base.BaseService;
import cn.chiship.sdk.framework.base.BaseController;
import cn.chiship.sdk.core.base.BaseResult;
import cn.chiship.sdk.core.util.StringUtil;
import cn.chiship.sdk.core.base.constants.BaseConstants;
import cn.chiship.sdk.core.annotation.Authorization;

import javax.annotation.Resource;
import javax.validation.Valid;

import cn.chiship.framework.business.biz.business.service.BusinessOrderHeaderService;
import cn.chiship.framework.business.biz.business.entity.BusinessOrderHeader;
import cn.chiship.framework.business.biz.business.entity.BusinessOrderHeaderExample;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 订单主表控制层
 * 2023/3/26
 *
 * @author lijian
 */
@RestController
@Authorization
@RequestMapping("/businessOrderHeader")
@Api(tags = "订单主表")
public class BusinessOrderHeaderController extends BaseController<BusinessOrderHeader, BusinessOrderHeaderExample> {

    private static final Logger LOGGER = LoggerFactory.getLogger(BusinessOrderHeaderController.class);

    @Resource
    private BusinessOrderHeaderService businessOrderHeaderService;

    @Override
    public BaseService getService() {
        return businessOrderHeaderService;
    }

    @SystemOptionAnnotation(describe = "订单主表分页")
    @ApiOperation(value = "订单主表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码", defaultValue = "1", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "limit", value = "每页条数", defaultValue = "10", required = true, dataTypeClass = Long.class, paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序方式", defaultValue = "-gmtModified", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "orderStatus", value = "订单状态", dataTypeClass = Byte.class, paramType = "query"),

    })
    @GetMapping(value = "/page")
    public ResponseEntity<BaseResult> page(
            @RequestParam(required = false, defaultValue = "", value = "keyword") String keyword,
            @RequestParam(required = false, defaultValue = "", value = "orderStatus") Byte orderStatus) {
        BusinessOrderHeaderExample businessOrderHeaderExample = new BusinessOrderHeaderExample();
        //创造条件
        BusinessOrderHeaderExample.Criteria businessOrderHeaderCriteria = businessOrderHeaderExample.createCriteria();
        businessOrderHeaderCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(keyword)) {
        }
        if (!StringUtil.isNull(orderStatus)) {
            businessOrderHeaderCriteria.andOrderStatusEqualTo(orderStatus);
        }
        return super.responseEntity(BaseResult.ok(super.page(businessOrderHeaderExample)));
    }

    @SystemOptionAnnotation(describe = "订单主表列表数据")
    @ApiOperation(value = "订单主表列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字", dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping(value = "/list")
    public ResponseEntity<BaseResult> list(@RequestParam(required = false, defaultValue = "", value = "keyword") String keyword) {
        BusinessOrderHeaderExample businessOrderHeaderExample = new BusinessOrderHeaderExample();
        //创造条件
        BusinessOrderHeaderExample.Criteria businessOrderHeaderCriteria = businessOrderHeaderExample.createCriteria();
        businessOrderHeaderCriteria.andIsDeletedEqualTo(BaseConstants.NO);
        if (!StringUtil.isNullOrEmpty(keyword)) {
        }
        return super.responseEntity(BaseResult.ok(super.list(businessOrderHeaderExample)));
    }


    @SystemOptionAnnotation(option = BusinessTypeEnum.SYSTEM_OPTION_REMOVE, describe = "删除订单主表")
    @ApiOperation(value = "删除订单主表")
    @PostMapping(value = "remove")
    public ResponseEntity<BaseResult> remove(@RequestBody @Valid List<String> ids) {
        BusinessOrderHeaderExample businessOrderHeaderExample = new BusinessOrderHeaderExample();
        businessOrderHeaderExample.createCriteria().andIdIn(ids);
        return super.responseEntity(super.remove(businessOrderHeaderExample));
    }

    @ApiOperation(value = "创建订单")
    @PostMapping(value = "/createOrder")
    public ResponseEntity<BaseResult> createOrder(@RequestBody @Valid OrderCreateDto orderCreateDto) {
        return new ResponseEntity(businessOrderHeaderService.createOrder(orderCreateDto, getLoginUser()), HttpStatus.OK);
    }

    @ApiOperation(value = "根据订单号查询订单状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单号", required = true, dataTypeClass = String.class, paramType = "query"),
    })
    @GetMapping(value = "/orderStatusByOrderId")
    public ResponseEntity<BaseResult> getOrderStatusByOrderId(@RequestParam(value = "orderId") String orderId) {
        return new ResponseEntity(businessOrderHeaderService.getOrderStatusByOrderId(orderId), HttpStatus.OK);
    }

    @SystemOptionAnnotation(describe = "二维码支付生成")
    @ApiOperation(value = "二维码支付生成")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", defaultValue = "", required = true, dataTypeClass = String.class, paramType = "query"),
            @ApiImplicitParam(name = "payMethod", value = "支付方式 alipay：支付宝  wxpay：微信", defaultValue = "alipay", required = true, dataTypeClass = String.class, paramType = "query"),

    })
    @GetMapping(value = "/generatePayQrCode")
    public ResponseEntity<BaseResult> generatePayQrCode(
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "payMethod") String payMethod) {
        return new ResponseEntity(businessOrderHeaderService.generatePayQrCode(orderId, payMethod), HttpStatus.OK);
    }

}